﻿// -----------------------------------------------------------------------
// <copyright file="TilePart.cs" company="R&R">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

#region Imports
using System;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows;
#endregion Imports

namespace Domino
{
    /// <summary>
    /// Interaction logic for TilePart.xaml
    /// </summary>
    public partial class TilePart : UserControl
    {
        #region Properties
        public int TileValue
        {
            get { return (int)GetValue(TileValueProperty); }
            set { SetValue(TileValueProperty, value); }
        }

        public static readonly DependencyProperty TileValueProperty =
            DependencyProperty.Register("TileValue", typeof(int), typeof(TilePart),
            new FrameworkPropertyMetadata(OnTileValueChanged));

        private static void OnTileValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var tilePart = d as TilePart;
            if (tilePart != null) tilePart.UpdateDots((int)e.NewValue);
        }
        #endregion Properties

        #region Constructors
        public TilePart()
        {
            InitializeComponent();
        }
        #endregion Constructors

        #region Methods
        private void UpdateDots(int tileValue)
        {
            string sequence = String.Empty;
            switch (tileValue)
            {
                case 0: break;
                case 1: sequence = "5"; break;
                case 2: sequence = "37"; break;
                case 3: sequence = "357"; break;
                case 4: sequence = "1379"; break;
                case 5: sequence = "13579"; break;
                case 6: sequence = "123789"; break;
                case 7: sequence = "1235789"; break;
                case 8: sequence = "12346789"; break;
                case 9: sequence = "123456789"; break;

                case -1: return;
                default: throw new ArgumentOutOfRangeException();
            }

            itmsCtrl.Items.Clear();
            for (int i = 1; i <= 9; i++)
            {
                var dot = new Dot
                {
                    HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch,
                    VerticalAlignment = System.Windows.VerticalAlignment.Stretch,
                    Visibility = (sequence.ToCharArray().Where(c => c.ToString() == i.ToString()).Any()
                        ? Visibility.Visible : Visibility.Hidden)
                };

                dot.Height = dot.Width = 10;
                itmsCtrl.Items.Add(dot);
            }
        }
        #endregion Methods
    }
}
